package com.mtbu.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.springframework.context.support.StaticApplicationContext;

import my.myorm.tool.jdbc.ResultList;
import my.myorm.tool.jdbc.SQLExecutor;
import my.myorm.tool.jdbc.SQLManager;
import my.myorm.tool.orm.TableBeanMap;

import com.mtbu.bean.Project;
import com.mtbu.bean.Step;
import com.mtbu.bean.SysUser;
import com.mtbu.dao.base.ProjectDaoBase;

import org.slf4j.*;
public final class  ProjectDao extends ProjectDaoBase{
	
	static final Logger logger = LoggerFactory.getLogger(ProjectDao.class);
	
   public String getSplit(Project project) throws Exception{
        return "";
    }
   
	//找出属于某用户有加入权限的所有project
   public List<Project> findChannelJoinAllowProjects(SysUser user) throws Exception{
		
		List<Project> res = new ArrayList<Project>();
		
		//找出用户有哪些step权限
		String stepPermission = user.getStepPermission();
		
		if (stepPermission.isEmpty() == false) {
			String sql=" select * from step where type=2 and id in (" + stepPermission + ")" ;
			SQLExecutor dbconn= SQLManager.getSQLExecutor(super.getDsName(),true);
			
			ResultList rs = dbconn.query(sql, false, null);
			if (rs.size() > 0) {
				
				String prjIds = "";
				for (int i = 0 ; i < rs.size() ; i++) {
					Step tmp = rs.get(i).get(Step.class);
					prjIds += tmp.getProjectId() + ",";
				}
				
				if (prjIds.isEmpty() == false) {
					prjIds = prjIds.substring(0,prjIds.length() -1);
				}
				
				sql = " select * from project where id in (" + prjIds + ")" ;
				rs = dbconn.query(sql, false, null);
				for (int i = 0 ; i < rs.size() ; i++) {
					res.add(rs.get(i).get(Project.class));
				}
				
				return res;
			}
			else{
				return res;
			}
		}
		else{
			return res;
		}       
	}
   
   public List<Project>  findAll() throws Exception{
	    
	   List<Project> res = new ArrayList<Project>();
	   
	   String sql="select * from " + getTable() ;
	   
	   SQLExecutor dbconn= SQLManager.getSQLExecutor(super.getDsName(),true);
	   
	   ResultList rs = dbconn.query(sql, false, null);
		for (int i = 0 ; i < rs.size() ; i++) {
			res.add(rs.get(i).get(Project.class));
		}
		
	   return res;
   }
}
